#include "Bomber.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../SGD Wrappers/SGD_String.h"
#include "../Game.h"

#define MAX_COOLDOWN 130.0f
CBomber::CBomber()
{
	
	m_fSprint = 70;
	m_fDash = 240;
	m_fMaxStamina = 110;
	m_fCurrStamina = 110;
	m_fCooldown = MAX_COOLDOWN;

}


CBomber::~CBomber()
{
}

void CBomber::Update(float elapsedTime)
{
	Time += elapsedTime;
	if (m_fCurrStamina < 1)
	{
		m_bExhausted = true;
		if (m_bCarrying == true)
		{
			m_bCarrying = false;
		}
	}

	if (m_fCurrStamina > 60)
	{
		m_bExhausted = false;
	}
	if (SGD::InputManager::GetInstance()->IsControllerConnected(m_iconnum))
		Input();
	CPlayer::Update(elapsedTime);

	if (MAX_COOLDOWN > GetCooldown())
	{
		//cooldown bar for using abilities.
		SetCooldown(GetCooldown() + 10 * elapsedTime);
		//Have we used any abilities?
		if (GetCDFlag())
		{
			//Has the delay for ability usage expired?
			if ((GetAbilityDelay() <= 0.0f))
			{
				//Allow us to use abilites again.
				SetAbilityDelay(10);
				SetCDFlag(false);
			}
			else
			{
				//Decrement ability Delay timer.
				SetAbilityDelay(GetAbilityDelay() - elapsedTime);
			}
		}
	}
	/*if (SGD::InputManager::GetInstance()->IsControllerConnected(m_iconnum))
		Input();*/
}

void CBomber::Render(void)
{
	if (m_bCarrying == true)
	{
		//ball carrying indicator
		SGD::GraphicsManager::GetInstance()->DrawRectangle({ SGD::Point{ (m_ptPosition.x - Game::GetInstance()->GetCameraPos().x - 20), (m_ptPosition.y - Game::GetInstance()->GetCameraPos().y - 30) },
			m_szSize * 2 }, SGD::Color{ 255, 175, 0 }, { 0, 0, 0 }, 3);
	}

	//SGD::GraphicsManager::GetInstance()->DrawRectangle({ SGD::Point{ (m_ptPosition.x - Game::GetInstance()->GetCameraPos().x), (m_ptPosition.y - Game::GetInstance()->GetCameraPos().y) }, m_szSize },
	//	SGD::Color{ 255, 0, 0 }, { 0, 0, 0 }, 3);

	SGD::GraphicsManager::GetInstance()->DrawTexture(Game::GetInstance()->GetArrow(), { m_ptPosition.x, m_ptPosition.y + m_szSize.height / 2 }, m_fRotation, { 112, 112 }, {}, { 0.4f, 0.4f });
	//Ability bar
	CPlayer::Render();

	//Cooldown bar
	SGD::Rectangle temp = this->GetAbilityRect();
	temp.top = temp.bottom - 10;
	temp.right = GetCooldown();

	if (GetCooldown() < MAX_COOLDOWN)
	{
		SGD::GraphicsManager::GetInstance()->DrawRectangle(temp, { 255, 255, 255 });
		float dProgress = ((float)GetCooldown() / MAX_COOLDOWN)*100.0f;
		SGD::OStringStream buffer;
		buffer << "Ability CD:" << (int)dProgress << "%";
		SGD::GraphicsManager::GetInstance()->DrawString(buffer.str().c_str(), { temp.left, temp.bottom });
	}

}

void CBomber::Input()
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();
	SGD::Vector c = pInput->GetLeftJoystick(GetControllernumber());

	bool sprinting = false;

	if (pInput->IsButtonDown(GetControllernumber(), 5))
	{
		if (m_bExhausted == false && m_bCarrying == false)
		{
			m_fCurrStamina -= 0.01f;
			sprinting = true;
		}
	}
	else
	{
		if (m_fCurrStamina < m_fMaxStamina)
			m_fCurrStamina += 0.01f;
	}

	if (pInput->IsButtonPressed(0, 0))
	{
		m_ptPosition.x += 10;
	}
	if (pInput->IsKeyDown(SGD::Key::W))
	{
		m_vtVelocity = { 0, -64 };
	}
	else if (pInput->IsKeyDown(SGD::Key::S))
	{
		m_vtVelocity = { 0, 64 };
	}
	else if (pInput->IsKeyDown(SGD::Key::A))
	{
		m_vtVelocity = { -64, 0 };
	}
	else if (pInput->IsKeyDown(SGD::Key::D))
	{
		m_vtVelocity = { 64, 0 };
	}

	if (pInput->IsKeyDown(SGD::Key::NumPad4))
	{
		SetCooldown(0);
	}

	if (c.y == 0 && c.x <= 0.5 && c.x >= 0 &&
		pInput->IsKeyDown(SGD::Key::W) == false
		&& pInput->IsKeyDown(SGD::Key::A) == false
		&& pInput->IsKeyDown(SGD::Key::S) == false
		&& pInput->IsKeyDown(SGD::Key::D) == false)
	{
		if (m_bPlayerStun == false)
		m_vtVelocity = { 0, 0 };
		if (this->currAnimation.GetName() != "bomberIdleB" && m_bTeam == false)
			CAnimationManager::GetInstance()->SetOwner(this, "bomberIdleB");
		else if (this->currAnimation.GetName() != "bomberIdleR" && m_bTeam == true)
			CAnimationManager::GetInstance()->SetOwner(this, "bomberIdleR");
	}
	else
	{
		if (this->currAnimation.GetName() != "bomberWalkB" && m_bTeam == false)
			CAnimationManager::GetInstance()->SetOwner(this, "bomberWalkB");
		else if (this->currAnimation.GetName() != "bomberWalkR" && m_bTeam == true)
			CAnimationManager::GetInstance()->SetOwner(this, "bomberWalkR");
	}


	if (m_bSprinting && m_bDash == false)
		CPlayer::Input(m_fSprint);
	else if (m_bSprinting == false && m_bDash == true)
	{
		CPlayer::Input(m_fDash);
	}
	else
	{
		if (m_bExhausted == true)
			m_fSpeed = 14;
		else
			m_fSpeed = 28;

		CPlayer::Input(m_fSpeed);
	}

	if (!GetCDFlag())
	{
		if (m_nButton == 2)
		{
			// they used the X Ability DO STUFF
			// Reset the button so it doesn't do the ability again
			m_nButton = 0;
			if (m_fCharging < 30)
			{
				//use the first level
				SetCDFlag(true);
				SetCooldown(0);

				// take away from the ability meter by 10 the lowest charge
				//if they just tap the ability button they can use a level 1 if they have it
				m_fAbilityCharge -= 10;

				//reset the charging meter to 0
				m_fCharging = 0;

				m_nPowerLevel = 1;
			}
			else if (m_fCharging >= 30 && m_fCharging < 60 && GetCharge() >= 59)
			{
				//use level 2
				SetCDFlag(true);
				SetCooldown(0);


				// take away from the ability meter by which level they used
				m_fAbilityCharge -= 30;

				//reset the charging meter to 0
				m_fCharging = 0;

				m_nPowerLevel = 2;
			}
			else if (m_fCharging >= 59)
			{
				//use level 3
				SetCDFlag(true);
				SetCooldown(0);


				// take away from the ability meter by which level they used
				m_fAbilityCharge -= 60;

				//reset the charging meter to 0
				m_fCharging = 0;

				m_nPowerLevel = 3;
			}
		}
		SuperThrow();
	}
	if (!GetCDFlag())
	{

		if (m_nButton == 3)
		{
			// they used the Y Ability DO STUFF
			// Reset the button so it doesn't do the ability again
			m_nButton = 0;
			if (m_fCharging <= 30)
			{
				//use the first level
				SetCDFlag(true);
				SetCooldown(0);

				// take away from the ability meter by 10 the lowest charge
				//if they just tap the ability button they can use a level 1 if they have it
				m_fAbilityCharge -= 10;

				//reset the charging meter to 0
				m_fCharging = 0;

				m_nPowerLevel = 1;
				PersonThrow();

			}
			else if (m_fCharging > 30 && m_fCharging < 60 && GetCharge() > 30)
			{
				//use level 2
				SetCDFlag(true);
				SetCooldown(0);

				// take away from the ability meter by which level they used
				m_fAbilityCharge -= 30;

				//reset the charging meter to 0
				m_fCharging = 0;

				m_nPowerLevel = 2;
				PersonThrow();

			}
			else if (m_fCharging >= 59)
			{
				//use level 3
				SetCDFlag(true);
				SetCooldown(0);

				// take away from the ability meter by which level they used
				m_fAbilityCharge -= 60;

				//reset the charging meter to 0
				m_fCharging = 0;

				m_nPowerLevel = 3;
				PersonThrow();

			}
		}
	}

}
/*virtual*/ void CBomber::HandleCollision(const Entity* pOther)	/*override*/
{
	CPlayer::HandleCollision(pOther);
}

void CBomber::SuperThrow()
{
	if (m_bCarrying == true)
	{
		SGD::InputManager* pInput = SGD::InputManager::GetInstance();
		SGD::Vector LeftAnalog = pInput->GetLeftJoystick(GetControllernumber());

		float LX = LeftAnalog.x;
		float LY = LeftAnalog.y;

		float x1 = LX;
		float y1 = LY;

		float dx = 4 * x1;
		float dy = 4 * y1;

		SGD::Vector v = { dx, dy };
				
		if (m_nPowerLevel == 1)
		{			
			v *= 100;

			m_pBall->SetDestPos({ m_ptPosition.x + v.x, m_ptPosition.y + v.y });
			m_pBall->SetVelocity({ (v.x / 2) + 10, (v.y / 2) + 10 });
			m_pBall->SetAerialVel({ m_pBall->GetVelocity().x, m_pBall->GetVelocity().y - 50 });
			m_pBall->SetShadowPos({ m_pBall->GetPosition().x, m_pBall->GetPosition().y });
			m_pBall->SetAerial(true);
			m_pBall->SetPowerLevel(1);
			m_bCarrying = false;
		}
		else if (m_nPowerLevel == 2)
		{
			v *= 200;

			m_pBall->SetDestPos({ m_ptPosition.x + v.x, m_ptPosition.y + v.y });
			m_pBall->SetVelocity({ (v.x / 2) + 10, (v.y / 2) + 10 });
			m_pBall->SetAerialVel({ m_pBall->GetVelocity().x, m_pBall->GetVelocity().y - 50 });
			m_pBall->SetShadowPos({ m_pBall->GetPosition().x, m_pBall->GetPosition().y });
			m_pBall->SetAerial(true);
			m_pBall->SetPowerLevel(2);
			m_bCarrying = false;
		}
		else if (m_nPowerLevel == 3)
		{
			v *= 300;
			
			m_pBall->SetDestPos({ m_ptPosition.x + v.x, m_ptPosition.y + v.y });
			m_pBall->SetVelocity({ (v.x / 2) + 10, (v.y / 2) + 10 });
			m_pBall->SetAerialVel({ m_pBall->GetVelocity().x, m_pBall->GetVelocity().y - 50 });
			m_pBall->SetShadowPos({ m_pBall->GetPosition().x, m_pBall->GetPosition().y });
			m_pBall->SetAerial(true);
			m_pBall->SetPowerLevel(3);
			m_bCarrying = false;
		}

		

		m_nPowerLevel = 0;
	}
}

void CBomber::PersonThrow()
{
	m_bThrowAbility = true;
}

void CBomber::BombThrow()
{


	m_nPowerLevel = 0;
}